package common.listener;

import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import common.ChanageDocToPdf;

public class ContextListener implements ServletContextListener,ServletRequestAware{
	private HttpServletRequest request;
    //监听容器销毁时需要的操作
	public void contextDestroyed(ServletContextEvent context) {
		System.out.println("--------------服务器关闭");
		String baseUrl=context.getServletContext().getRealPath("/uploads");
        File sourcePathFile=new File(baseUrl);
        changeAllWordToPdf(sourcePathFile);
	}
	
    //监听容器初始化时需要加载的东西
	public void contextInitialized(ServletContextEvent context) {
          System.out.println("------------------服务器启动");
	}
	private void changeAllWordToPdf(File sourcePathFile) {
		File sourceFilesList[] = sourcePathFile.listFiles();
		for(int i=0;i<sourceFilesList.length;i++){
			if(sourceFilesList[i].isDirectory()){
				try {
					changeAllWordToPdf(sourceFilesList[i]);
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
			}else if(sourceFilesList[i].isFile()){
				//取得文件名字
				String fileName=sourceFilesList[i].getName();
				//取得文件的扩展名
				String fileType=fileName.substring((fileName.length() - 4),fileName.length());
				if(fileType.equals("docx")){
					String destiantionFilePath=sourceFilesList[i].getPath();
					System.out.println("==========="+destiantionFilePath);
					File file=new File(destiantionFilePath.replace("docx","pdf"));
					//判断该word文档是否已经在服务器端转换为pdf文档，如果没有转换则进行转换
					if(!file.exists()){
						ChanageDocToPdf.chanage(destiantionFilePath,destiantionFilePath.substring(0, destiantionFilePath.length()-5),17);
					}
				}else if(fileType.equals(".doc")){
					String destiantionFilePath=sourceFilesList[i].getPath();
					try {
						InetAddress addr=InetAddress.getLocalHost();
						String ip=addr.getHostAddress();
						System.out.println(ip);
						System.out.println(addr.getHostName());
						System.out.println(addr.getAddress());
					} catch (UnknownHostException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					String baseUrl=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
					//String baseUrl="http://localhost:8080/wrem/";
					String sourceFilePath=baseUrl+destiantionFilePath.substring(destiantionFilePath.lastIndexOf("uploads"),destiantionFilePath.length());
					System.out.println("-----------"+destiantionFilePath);
					File file=new File(destiantionFilePath.replace("doc","pdf"));
					//判断该word文档是否已经在服务器端转换为pdf文档，如果没有转换则进行转换
					if(!file.exists()){
						ChanageDocToPdf.chanage(sourceFilePath,destiantionFilePath.substring(0, destiantionFilePath.length()-4),17);
					}
				}
			}
		}
	}
	public void setServletRequest(HttpServletRequest request) {
		// TODO Auto-generated method stub
		this.request=request;
	}

}
